//
//  ZWVisitorView.swift
//  weiboXue
//
//  Created by xue-iOS-work on 16/8/30.
//  Copyright © 2016年 iOS-work. All rights reserved.
//

import UIKit

class ZWVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
       
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //------初始化    添加约束.
    func setupUI(){
        // ---- 背景颜色
         backgroundColor = UIColor(white: 237/255, alpha: 1)
        // -----添加控件
        addSubview(midView)
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(titleLabel)
        addSubview(zhuCeBtn)
        addSubview(loginBtn)
        //约束
        iconView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        midView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        titleLabel.snp_makeConstraints { (make) in
            make.top.equalTo(midView.snp_bottom).offset(12)
            make.centerX.equalTo(self)
            make.width.equalTo(224)
        }
        zhuCeBtn.snp_makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp_bottom).offset(6)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.right.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp_bottom).offset(6)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        maskIconView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(zhuCeBtn)
        }
        
    }
    
    
    //----懒加载控件
    // 中心视图
    lazy var iconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    // 内圈视图
    lazy var midView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    // 文本框
    lazy var titleLabel: UILabel = {
        let label = UILabel(labelColor: UIColor.darkGray, fontSize: 14)
        label.text = "gggggggggggggggggggggggggggggg"
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    // 注册按钮
    lazy var zhuCeBtn: UIButton = {
        let btn = UIButton(textNormalColor: UIColor.darkGray, textHighlightedColor: UIColor.orange, fontsize: 14, titleName: "注册")
        // 设置btn的背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    // 登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton(textNormalColor: UIColor.darkGray, textHighlightedColor: UIColor.orange, fontsize: 14, titleName: "登录")
        // 设置btn的背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    // 半透明视图
    private lazy var maskIconView = UIImageView (image:#imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //---- 提供一个方法换中间的图片
    func setupinfo( imageName:UIImage?, titleName:String) {
        titleLabel.text = titleName
        if imageName != nil {
            iconView.image = imageName
            midView.isHidden = true
        }else{
            // 做动画
            startAnimation()
        }
    }
    //-------定义一个方法做动画
    func startAnimation() {
        // 定义一个动画 设置路径
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置一圈
        anim.toValue = 2*M_PI
        // 重复一次
        anim.repeatCount = MAXFLOAT
        // 动画时间
        anim.duration = 15
        // 切换的时候不移除动画
        anim.isRemovedOnCompletion = false
        // 将动画添加到layar
        midView.layer.add(anim, forKey: nil)
        
    }
    
}

